package ssm.service;

import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ssm.dao.*;
import ssm.model.PersonMapper;
import ssm.model.TrackCarMapper;
import ssm.model.YhzpbMapper;
import ssm.model.YktCarinfoMapper;
import ssm.vo.TrackCarAddPersonVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TrackCarServiceImpl implements TrackCarService {

    @Autowired
    private TrackCarMapper trackCarMapper;
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private YhzpbMapper yhzpbMapper;
    @Autowired
    private YktCarinfoMapper yktCarinfoMapper;

    public List<TrackCar> get() {
        List<TrackCar> cars = trackCarMapper.get();
        return cars;
    }

    public Map<String,Object> getOneByTime(String cph, String starttime, String endtime,int page,int pageNum) {

        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String,Object>();
        if(page!=-1&&pageNum!=0){
            int pageStart=(page)*pageNum;
            param.put("pageStart", pageStart);
            param.put("pageEnd", pageNum);
        }
        param.put("cph",cph);
        param.put("starttime",starttime);
        param.put("endtime",endtime);
        List<TrackCar> cars = trackCarMapper.getOneByTime(param);
        List<Map<String, Object>> list = toMap(cars);
        int total=trackCarMapper.getCountByTime(param);
        result.put("data",list);
        result.put("total",total);
        return result;
    }

    public List<Map<String, Object>> getByMap(String cph, String starttime, String endtime) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!(cph == null || cph.equals(""))) {
            map.put("cph", cph);
        }
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        List<TrackCar> cars = trackCarMapper.selectAll(map);
        List<Map<String, Object>> list = toMap(cars);
        return list;
    }


    public Map<String,Object> getByTimeOrJid(List<String> list,
                                             String cph,
                                             String starttime,
                                             String endtime,
                                             int page,
                                             int pageNum,
                                             Integer type,
                                             String name,
                                             String jid) {
        Map<String, Object> param = new HashMap<String, Object>();
        Map<String, Object> result = new HashMap<String, Object>();


        if (!list.isEmpty()) {
            param.put("list", list);
        }
        param.put("starttime", starttime);
        param.put("endtime", endtime);

        if (page != -1 && pageNum != 0) {
            int pageStart=(page)*pageNum;
            param.put("pageStart", pageStart);
            param.put("pageEnd", pageNum);
        }
        //提高查询要素优先级
        if(!StringUtils.isNullOrEmpty(name)||!StringUtils.isNullOrEmpty(jid)){
            System.out.println("校内:"+param);
            if (name != null && !name.equals("")) {
                param.put("name", name);
            }
            if (jid != null && !jid.equals("")) {
                param.put("jid", jid);
            }
            List<TrackCarAddPersonVo> vos=trackCarMapper.getCarAndPerson(param);
            for(TrackCarAddPersonVo vo:vos){
                Yhzpb yhzpb = yhzpbMapper.findByJid(vo.getJid());
                if (yhzpb != null) {
                    vo.setImage(yhzpb.getZp());
                }
            }
            int total=trackCarMapper.getCarAndPersonTotal(param);
            result.put("data",vos);
            result.put("total",total);
            return result;
        }


        if (cph != null && !cph.equals("")) {
            System.out.println(11);
            param.put("cph",cph);
            List<TrackCarAddPersonVo> vos=trackCarMapper.getCarAndPerson(param);
           if(!vos.isEmpty()){
               System.out.println(vos);
               for(TrackCarAddPersonVo vo:vos){
                   Yhzpb yhzpb = yhzpbMapper.findByJid(vo.getJid());
                   if (yhzpb!=null) {
                       vo.setImage(yhzpb.getZp());
                   }
               }
               int total=trackCarMapper.getCarAndPersonTotal(param);
               result.put("data",vos);
               result.put("total",total);
               System.out.println("校内车辆:result+cph:"+result);
               return result;
           }else{
               System.out.println(22);
               List<TrackCar>  outCars = trackCarMapper.selectOutCars(param);
              if(!outCars.isEmpty()){
                  int total=trackCarMapper.getTotal(param);
                  result.put("data",outCars);
                  result.put("total",total);
                  System.out.println("校外车辆:result+cph:"+result);
                  return result;
              }
           }
        }

        //********************校外车辆
        System.out.println(33);
        System.out.println("校外:"+param);
        if(type==0){
            List<TrackCar>  outCars = trackCarMapper.selectOutCars(param);
            int total=trackCarMapper.getTotal(param);
            result.put("data",outCars);
            result.put("total",total);
            return result;
        }
        //********************校内车辆
        System.out.println(44);
        System.out.println("校内:"+param);
        if (name != null && !name.equals("")) {
            param.put("name", name);
        }
        if (jid != null && !jid.equals("")) {
            param.put("jid", jid);
        }
        List<TrackCarAddPersonVo> vos=trackCarMapper.getCarAndPerson(param);
        for(TrackCarAddPersonVo vo:vos){
            Yhzpb yhzpb = yhzpbMapper.findByJid(vo.getJid());
            if (yhzpb != null) {
                vo.setImage(yhzpb.getZp());
            }
        }
        int total=trackCarMapper.getCarAndPersonTotal(param);
        result.put("data",vos);
        result.put("total",total);
        return result;

//        if(type==1){
//            List<TrackCarAddPersonVo> vos=new ArrayList<TrackCarAddPersonVo>();
//            for(TrackCar car:cars){
//                TrackCarAddPersonVo vo=new TrackCarAddPersonVo(car);
//                String jid=yktCarinfoMapper.selectJidhByCph(car.getCphm());
//                PersonBase personBase = personMapper.findByJid(jid);
//                if (personBase != null) {
//                    Yhzpb yhzpb = yhzpbMapper.findByJid(jid);
//                    if (yhzpb != null) {
//                        personBase.setZp(yhzpb.getZp());
//                    }
//                }
//                vo.setPersonbase(personBase);
//                vos.add(vo);
//            }
//            result.put("data",vos);
//        }else{
//            result.put("data",cars);
//        }
//

//        return result;
    }

    public Map<String, Object> findByMap(Map<String, Object> param) {
        Map<String,Object>result=new HashMap<String,Object>();
        List<TrackCar> cars = trackCarMapper.selectAll(param);
        List<Map<String, Object>> list = toMap(cars);
        result.put("data",list);
        int total=trackCarMapper.getTotal(param);
        result.put("total",total);
        return result;
    }

    public List<Map<String, Object>> getByTime(String starttime, String endtime) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("starttime", starttime);
        map.put("endtime", endtime);
        List<TrackCar> cars = trackCarMapper.selectAll(map);
        List<Map<String, Object>> list = toMap(cars);
        return list;
    }


    public List<Map<String, Object>> toMap(List<TrackCar> cars) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (TrackCar trackCar : cars) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("carPlate", trackCar.getCphm());
            map.put("passTime", trackCar.getJgsj());
            map.put("plateColor", trackCar.getCpys());
            map.put("direction", trackCar.getCdfx());
            map.put("sbbh", trackCar.getSbbh());
            map.put("zyb", trackCar.getZyb());
            map.put("kkmc", trackCar.getKkmc());
            map.put("clpp", trackCar.getClpp());
            map.put("jgdd", trackCar.getJgdd());
            map.put("xszt", trackCar.getXszt());
            map.put("cs", trackCar.getCs());
            map.put("cplx", trackCar.getCplx());
            map.put("zpp", trackCar.getZpp());
            map.put("cllx", trackCar.getCllx());
            map.put("zp", trackCar.getZp());
            map.put("belong", trackCar.getBelong());
            map.put("clys", trackCar.getClys());
            map.put("camerJd", trackCar.getCamerjd());
            map.put("camerWd", trackCar.getCamerwd());
            // map.put("barrName",trackCar.getJgdd());
            list.add(map);
        }
        return list;
    }

}
